package com.chaychan.bottombarlayout_ohos.slice;

import com.chaychan.bottombarlayout_ohos.ResourceTable;
import com.chaychan.library.BottomBarLayout;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.ability.fraction.Fraction;
import ohos.aafwk.ability.fraction.FractionAbility;
import ohos.aafwk.ability.fraction.FractionScheduler;
import ohos.aafwk.content.Intent;
import ohos.agp.components.*;
import ohos.app.dispatcher.task.TaskPriority;

import java.util.ArrayList;
import java.util.List;

public abstract class BaseViewPagerActivity extends AbilitySlice {

    private PageSlider mVpContent;
    protected BottomBarLayout mBottomBarLayout;

    private FractionScheduler fractionScheduler;
    private List<TabFragment> mFragmentList = new ArrayList<>();
//    private List<Component> mFragmentList = new ArrayList<>();

    protected abstract String[] getFragmentContents();


    protected abstract int getLayoutResId();

    protected abstract String getTitle();

    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(getLayoutResId());
        initView();
        initData();
        initListener();

    }


    public void initView() {
        mVpContent = (PageSlider) findComponentById(ResourceTable.Id_vp_content);
        mBottomBarLayout = (BottomBarLayout) findComponentById(ResourceTable.Id_bbl);
        Text text = (Text) findComponentById(ResourceTable.Id_titleTv);
        text.setText(getTitle());
        Image image = (Image) findComponentById(ResourceTable.Id_menuImg);
        image.setClickedListener(component -> {
            MenuDialog menuDialog = new MenuDialog(getContext());
            menuDialog.setonMenuSelectedListener(new MenuDialog.OnMenuSelectedListener() {
                @Override
                public void onMenuSelected(int position) {

                    switch (position) {
                        case 0:
                            mBottomBarLayout.setUnread(0, 0);
                            mBottomBarLayout.setUnread(1, 0);
                            break;
                        case 1:
                            mBottomBarLayout.hideNotify(2);
                            break;
                        case 2:
                            mBottomBarLayout.hideMsg(3);
                            break;
                    }
                }
            });
            menuDialog.show();
        });
    }


    private void initData() {

        fractionScheduler = ((FractionAbility) getAbility()).getFractionManager()
                .startFractionScheduler();
        mFragmentList.clear();
        for (String tabContent : getFragmentContents()) {
            TabFragment fragment = new TabFragment(this, tabContent);
            mFragmentList.add(fragment);
        }


//        for (String tabContent : getFragmentContents()) {
//            Component component = LayoutScatter.getInstance(getContext()).parse(ResourceTable.Layout_fraction_tab, null, false);
//            Text text = (Text) component.findComponentById(ResourceTable.Id_text);
//            text.setTextSize(15, Text.TextSizeType.FP);
//            text.setText(tabContent);
//            mFragmentList.add(component);
//        }
    }

    public void initListener() {
        mVpContent.setProvider(new MyAdapter());
        mBottomBarLayout.setViewPager(mVpContent);

        getGlobalTaskDispatcher(TaskPriority.DEFAULT).delayDispatch(new Runnable() {
            @Override
            public void run() {
                getMainTaskDispatcher().syncDispatch(new Runnable() {
                    @Override
                    public void run() {
                        mBottomBarLayout.setUnread(0, 20);//设置第一个页签的未读数为20
                        mBottomBarLayout.setUnread(1, 1001);//设置第二个页签的未读数
                        mBottomBarLayout.showNotify(2);//设置第三个页签显示提示的小红点
                        mBottomBarLayout.setMsg(3, "NEW");//设置第四个页签显示NEW提示文字
                    }
                });
            }
        }, 500);


    }


    class MyAdapter extends PageSliderProvider {
        @Override
        public int getCount() {
            return mFragmentList.size();
        }

        @Override
        public Object createPageInContainer(ComponentContainer componentContainer, int i) {

            int childCount = componentContainer.getChildCount();

            int id = componentContainer.getId();
            componentContainer.setId(10200312 * (i + 1));
            ((FractionAbility) getAbility()).getFractionManager()
                    .startFractionScheduler().replace(componentContainer.getId(), mFragmentList.get(i))
                    .submit();
            return componentContainer;

//            componentContainer.addComponent(mFragmentList.get(i));
//            return componentContainer;
        }

        @Override
        public void destroyPageFromContainer(ComponentContainer componentContainer, int i, Object o) {
//            if (i < mFragmentList.size()) {
//                componentContainer.removeComponent(mFragmentList.get(i));
//            }

            if (i < mFragmentList.size()) {
                componentContainer.removeComponent(mFragmentList.get(i).getComponent());
            }
        }

        @Override
        public boolean isPageMatchToObject(Component component, Object o) {
//            return component == o;
            return component == ((Fraction) o).getComponent();
        }

    }

}
